//
//  SettingOptionController.m
//  RuiHeApp
//
//  Created by Jun on 2022/11/30.
//

#import "SettingOptionController.h"
#import "RHWindowTipManage.h"


@interface SettingOptionController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic ,strong) UIPickerView *pickerView;

@property (nonatomic ,copy) NSArray *array;

@end

@implementation SettingOptionController

- (instancetype)initWithData:(NSArray *)dataArray{
    
    self = [super init];
    if (self) {
        self.array = dataArray;
    }
    return self;
}

- (NSArray *)array{
    if (!_array) {
        _array = [NSArray array];
    }
    return _array;
}

-(void)setCurTextValue:(NSInteger)curTextValue
{
    _curTextValue = curTextValue;
    __block NSInteger index = -1;
    [self.array enumerateObjectsUsingBlock:^(DevSettingOptionModel  *_Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(model.textValue == curTextValue){
            index = idx;
            *stop = YES;
        }
    }];
    if(index != -1){
        [self.pickerView selectRow:index inComponent:0 animated:NO];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    
    [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:touchView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:FontColor(@"#f5f5f5")];
    [self.view addSubview:bgView];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:FontColor(@"#000000") forState:UIControlStateNormal];
    [btn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [bgView addSubview:btn];

    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(35);
        make.right.mas_offset(-15);
        make.width.mas_equalTo(65);
        make.centerY.mas_offset(0);
    }];
    
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kUI_WINDOW_H-(216+kTabbarSafeBottomMargin), kUI_WINDOW_W, 216+kTabbarSafeBottomMargin)];
    [_pickerView setDataSource:self];
    [_pickerView setDelegate:self];
    [_pickerView setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:_pickerView];
    
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(45);
        make.left.right.mas_offset(0);
        make.bottom.equalTo(_pickerView.mas_top);
    }];
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];

    
    kWeakSelf;
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [weakSelf hidden];
    }];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.selectBlock) {
            weakSelf.selectBlock([weakSelf.pickerView selectedRowInComponent:0]);
        }
        [weakSelf hidden];
    }];
}

- (void)hidden{
    [[RHWindowTipManage shareWindowTipManage] hidden];
}


#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.array.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component API_UNAVAILABLE(tvos){
    DevSettingOptionModel *model = self.array[row];
    return model.text.rh_localized;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component API_UNAVAILABLE(tvos){
    
}

@end
